package com.example.budget3.modify.dialogs;


import java.util.Calendar;

import com.example.budget3.modify.RecurrTransaction;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TimePicker;

public class TimeDialogFragment extends DialogFragment {
	
	private TimePicker timePicker;
	private int hour;
	private int minute;
	private CheckBox includeToday;
	private boolean today;
	private String tag;

	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		if(savedInstanceState!=null ){
				today = savedInstanceState.getBoolean("today");
				hour = savedInstanceState.getInt("hour");
				minute = savedInstanceState.getInt("minute");
		}else{  
			Calendar calendar = ((RecurrTransaction) getActivity()).getRecCalendar();
			today = ((RecurrTransaction) getActivity()).getIncludeTimeToday();
			hour = calendar.get(Calendar.HOUR_OF_DAY);
	        minute = calendar.get(Calendar.MINUTE);
		}
		
		tag = getTag();
    	   

		
    	timePicker = new TimePicker(getActivity());
    	timePicker.setIs24HourView(false);
    	timePicker.setCurrentHour(hour);
   		timePicker.setCurrentMinute(minute);

		timePicker.setVisibility(View.VISIBLE);
		
		

   		LinearLayout linear =  new LinearLayout(getActivity());
   		linear.setGravity(Gravity.CENTER);
 	    linear.addView(timePicker);
 	    
		if(tag != "recurrEdit"){
			includeToday = new CheckBox(getActivity());
			includeToday.setText("Include Today");
			includeToday.setChecked(today);
	 	    
	 	    linear.addView(includeToday);
		}
 	    linear.setOrientation(LinearLayout.VERTICAL);
		
		AlertDialog.Builder adb = new AlertDialog.Builder( getActivity());
	    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	hour = timePicker.getCurrentHour();
            	minute = timePicker.getCurrentMinute();
            	if(tag != "recurrEdit"){
            		today = includeToday.isChecked();
            	}
            	((RecurrTransaction) getActivity()).setTime(minute,hour,today);
            }
        }).setNeutralButton("Reset",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {}
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {}
        });
		

	    AlertDialog dialogBox = adb.setView(linear).create();
	    dialogBox.show();
	    
	    Button positiveButton = dialogBox.getButton(DialogInterface.BUTTON_NEUTRAL);
	    positiveButton.setOnClickListener(new View.OnClickListener() {

	        @Override
	        public void onClick(View onClick) {                 
	        	final Calendar c = Calendar.getInstance();
        		hour = c.get(Calendar.HOUR_OF_DAY);
                minute = c.get(Calendar.MINUTE);
                if(tag != "recurrEdit"){
                	today = includeToday.isChecked();
                }
	        	timePicker.setCurrentHour(hour);
	       		timePicker.setCurrentMinute(minute);
	       		timePicker.invalidate();
	        }
	    });

		return dialogBox;

    }
	
	
	@Override
	public void onSaveInstanceState(Bundle arg0) {
    	hour = timePicker.getCurrentHour();
    	minute = timePicker.getCurrentMinute();
    	if(tag != "recurrEdit"){
	    	today = includeToday.isChecked();
			arg0.putBoolean("today", today);
    	}
		arg0.putInt("hour", hour);
		arg0.putInt("minute", minute);
		super.onSaveInstanceState(arg0);
	}

}
